package com.miracle9.game.service;

import com.miracle9.game.dao.*;
import com.miracle9.game.entity.*;
import com.miracle9.game.enums.GameTypeEnums;
import com.miracle9.game.server.message.AmfMessageSend;
import com.miracle9.game.server.message.StorageMethodEntity;
import com.miracle9.game.util.GameParameterAssist;
import com.miracle9.game.util.MyUtil;
import com.miracle9.game.util.SessionUtils;
import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("activityService")
public class ActivityService extends BaseService {

    private Logger logger;
    @Autowired
    private com.miracle9.game.dao.ActivityInfoServiceDao activityInfoServiceDao;
    @Autowired
    private UserActivityServiceDao userActivityServiceDao;
    @Autowired
    private UserServiceDao userServiceDao;
    @Autowired
    private com.miracle9.game.dao.ActivityServiceDao activityServiceDao;
    @Autowired
    private com.miracle9.game.dao.AdminLogServiceDao adminLogServiceDao;
    @Autowired
    private DeskServiceDao deskServiceDao;
    @Autowired
    private FishDeskServiceDao fishDeskServiceDao;
    @Autowired
    private CardDeskServiceDao cardDeskServiceDao;
    @Autowired
    private MermaidDeskServiceDao mermaidDeskServiceDao;
    @Autowired
    private LackDeskServiceDao lackDeskServiceDao;
    @Autowired
    private JoyDeskServiceDao joyDeskServiceDao;
    @Autowired
    private UserAwardServiceDao userAwardServiceDao;
    @Autowired
    private UserWarningServiceDao userWarningServiceDao;
    @Autowired
    private SystemConfigServiceDao systemConfigServiceDao;
    public static double rate0 = 0.2D;

    public ActivityService() {
        this.logger = Logger.getLogger(ActivityService.class);
    }

    public Map<String, Object> getActivityInfo(IoSession session) {
        Map result = new HashMap();
        ActivityInfo activityInfo = getActivityInfo();
        if (activityInfo != null) {
            result.put("activityInfo", activityInfo);
        }
        return result;
    }

    public ActivityInfo getActivityInfo() {
        ActivityInfo activityInfo = this.activityInfoServiceDao.getActivityInfo();
        if (activityInfo == null) {
            activityInfo = new ActivityInfo();
            activityInfo.setActivityStatus(0);
            activityInfo.setStartDate("0000-00-00 00:00:00");
            activityInfo.setEndDate("0000-00-00 00:00:00");
            this.activityInfoServiceDao.save(activityInfo);
        }
        return activityInfo;
    }

    public Activity[] getActivityList(IoSession session) {
        List<Activity> activities = getActivityList();
        for (Activity activity : activities) {
            if ((activity != null) && (resultInt(session) != 0)) {
                activity.setContent(activity.getContentEn());
            }
        }
        return activities.toArray(new Activity[0]);
    }

    public List<Activity> getActivityList() {
        List activities = this.activityServiceDao.getActivityList();
        return activities;
    }

    public Map<String, Object> addActivity(Activity activity, IoSession session) {
        Map<String, Object> result = new HashMap<>();
        this.activityServiceDao.saveOrUpdate(activity);
        result.put("success", Boolean.TRUE);
        result.put("message", "");
        return result;
    }

    public void addActivity(int nType, String strConditions, String strAwardGold, IoSession session) {
        Map result = new HashMap();
        int nTypeCount = this.activityServiceDao.getQuantityByTypeOrderby(nType);
        if (nTypeCount >= 21) {
            result.put("success", Boolean.valueOf(false));
            result.put("message", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "activityNumOverRun"));
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "addActivity", new Object[]{result}));
        } else {
            Activity activity = new Activity();
            List activities = new ArrayList();
            int nCondition1 = 0;
            int nCondition2 = 0;
            int nCondition3 = 0;
            String strContent = "";
            String strContent2 = "";
            String strContentEn = "";
            String strContentEn2 = "";
            String[] conditions = strConditions.split("\\|");
            String strDatetime = "";
            int nAwardGold = 0;
            activity.setType(nType);
            List<Activity> activityList = this.activityServiceDao.getActivitysByIdSQL(nType);
            int count = 0;
            Object strCondition1;
            switch (nType) {
                case 1:
                    nCondition1 = Integer.parseInt(conditions[0]);
                    nCondition2 = Integer.parseInt(conditions[1]);
                    activity.setCondition1(nCondition1);
                    activity.setCondition2(nCondition2);
                    activity.setCondition3(-1);
                    for (Activity a : activityList) {
                        if (a.getCondition1() == activity.getCondition1()) {
                            count++;
                        }
                    }
                    if (count > 0) {
                        result.put("success", Boolean.valueOf(false));
                        result.put("message", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "activityConditionsRepeat"));
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "addActivity", new Object[]{result}));
                        return;
                    }
                    nAwardGold = Integer.parseInt(strAwardGold);
                    activity.setAwardGold(nAwardGold);
                    strContent = "活动期间一次性充值" + nCondition1 + "游戏币且充值后总押达到" + nCondition2 + "游戏币，赠送" + nAwardGold + "游戏币";
                    strContentEn = "Recharge " + nCondition1 + " coins during the activity and the total bet reach " + nCondition2 + ", and you will be given " + nAwardGold + " coins as gift";
                    activity.setContent(strContent);
                    activity.setContentEn(strContentEn);
                    strDatetime = MyUtil.dateFormatYMD_HMS_Sting(new Date());
                    activity.setDatetime(strDatetime);
                    this.activityServiceDao.saveReturn(activity);
                    activities.add(activity);
                    break;
                case 2:
                    nCondition1 = Integer.parseInt(conditions[0]);
                    activity.setCondition1(nCondition1);
                    activity.setCondition2(-1);
                    activity.setCondition3(-1);
                    for (Activity a : activityList) {
                        if (a.getCondition1() == activity.getCondition1()) {
                            count++;
                        }
                    }
                    if (count > 0) {
                        result.put("success", Boolean.valueOf(false));
                        result.put("message", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "activityConditionsRepeat"));
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "addActivity", new Object[]{result}));
                        return;
                    }
                    nAwardGold = Integer.parseInt(strAwardGold);
                    activity.setAwardGold(nAwardGold);
                    strContent = "活动期间每日累计总押达到" + nCondition1 + "游戏币，赠送" + nAwardGold + "游戏币";
                    strContentEn = "If the amount of daily accumulation coins reach " + nCondition1 + " during the activity, and you will be given " + nAwardGold + " coins as gift";
                    activity.setContent(strContent);
                    activity.setContentEn(strContentEn);
                    strDatetime = MyUtil.dateFormatYMD_HMS_Sting(new Date());
                    activity.setDatetime(strDatetime);
                    this.activityServiceDao.saveReturn(activity);
                    activities.add(activity);
                    break;
                case 3:
                    nCondition1 = Integer.parseInt(conditions[0]);
                    activity.setCondition1(nCondition1);
                    activity.setCondition2(-1);
                    activity.setCondition3(-1);
                    for (Activity a : activityList) {
                        if (a.getCondition1() == activity.getCondition1()) {
                            count++;
                        }
                    }
                    if (count > 0) {
                        result.put("success", Boolean.valueOf(false));
                        result.put("message", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "activityConditionsRepeat"));
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "addActivity", new Object[]{result}));
                        return;
                    }
                    nAwardGold = Integer.parseInt(strAwardGold);
                    activity.setAwardGold(nAwardGold);
                    strContent = "活动期间达到Lv" + nCondition1 + "，赠送" + nAwardGold + "游戏币";
                    strContentEn = "If you are in level " + nCondition1 + " during activity, you will be given " + nAwardGold + " coins as gift";
                    activity.setContent(strContent);
                    activity.setContentEn(strContentEn);
                    strDatetime = MyUtil.dateFormatYMD_HMS_Sting(new Date());
                    activity.setDatetime(strDatetime);
                    this.activityServiceDao.saveReturn(activity);
                    activities.add(activity);
                    break;
                case 4:
                    int nTopAwardType = activity.getType();
                    nCondition1 = Integer.parseInt(conditions[0]);
                    strCondition1 = "";
                    String strCondition1En = "";
                    switch (nCondition1) {
                        case 0:
                            strCondition1 = "每日0点";
                            strCondition1En = "0 o' clock per day";
                            break;
                        case 1:
                            strCondition1 = "每周一0点";
                            strCondition1En = "0 o' clock on Monday";
                    }
                    nCondition2 = Integer.parseInt(conditions[1]);
                    String strCondition2 = "";
                    String strCondition2En = "";
                    GameTypeEnums gameType = GameTypeEnums.getByType(nCondition2);
                    if (gameType != null) {
                        strCondition2 = gameType.getName();
                        strCondition2En = gameType.getEname();
                    }
                    nCondition3 = Integer.parseInt(conditions[2]);
                    String strCondition3 = "";
                    String strCondition3En = "";
                    switch (nCondition3) {
                        case 0:
                            strCondition3 = "日排行";
                            strCondition3En = "Daily ranking";
                            break;
                        case 1:
                            strCondition3 = "周排行";
                            strCondition3En = "Weekly ranking";
                    }
                    activity.setCondition1(nCondition1);
                    activity.setCondition2(nCondition2);
                    activity.setCondition3(nCondition3);
                    int nCondition4 = nCondition1;
                    int nCondition5 = nCondition2;
                    int nCondition6 = nCondition3;
                    for (Activity a2 : activityList) {
                        if ((a2.getCondition1() == nCondition1) && (a2.getCondition2() == nCondition2) && (a2.getCondition3() == nCondition3)) {
                            count++;
                        }
                    }
                    if (count > 0) {
                        result.put("success", Boolean.valueOf(false));
                        result.put("message", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "activityConditionsRepeat"));
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "addActivity", new Object[]{result}));
                        return;
                    }
                    String[] strAwardGolds = strAwardGold.split("\\|");
                    for (int i = 0; i < 3; i++) {
                        String strLevelRange = "";
                        if (i == 0) {
                            activity = new Activity();
                            strLevelRange = "1";
                            nAwardGold = Integer.parseInt(strAwardGolds[i]);
                            strContent = "活动期间" + (String) strCondition1 + "在" + strCondition2 + "的" + strCondition3 + "榜达到第" + strLevelRange + "名，次日赠送" + nAwardGold + "游戏币";
                            strContentEn = "During activity " + strCondition1En + " in " + strCondition2En + " at " + strCondition3En + " reach the No." + strLevelRange + " and you will be given " + nAwardGold + "coins as gift the next day";
                            activity.setContent(strContent);
                            activity.setContentEn(strContentEn);
                            this.activityServiceDao.saveReturn(activity);
                            activities.add(activity);
                        } else if (i == 1) {
                            activity = new Activity();
                            strLevelRange = "2~3";
                            nAwardGold = Integer.parseInt(strAwardGolds[i]);
                            strContent = "活动期间" + (String) strCondition1 + "在" + strCondition2 + "的" + strCondition3 + "榜达到第" + strLevelRange + "名，次日赠送" + nAwardGold + "游戏币";
                            strContentEn = "During activity " + strCondition1En + " in " + strCondition2En + " at " + strCondition3En + " reach the No." + strLevelRange + " and you will be given " + nAwardGold + "coins as gift the next day";
                            activity.setContent(strContent);
                            activity.setContentEn(strContentEn);
                            this.activityServiceDao.saveReturn(activity);
                            activities.add(activity);
                        } else if (i == 2) {
                            activity = new Activity();
                            strLevelRange = "4~10";
                            nAwardGold = Integer.parseInt(strAwardGolds[i]);
                            strContent = "活动期间" + (String) strCondition1 + "在" + strCondition2 + "的" + strCondition3 + "榜达到第" + strLevelRange + "名，次日赠送" + nAwardGold + "游戏币";
                            strContentEn = "During activity " + strCondition1En + " in " + strCondition2En + " at " + strCondition3En + " reach the No." + strLevelRange + " and you will be given " + nAwardGold + "coins as gift the next day";
                            activity.setContent(strContent);
                            activity.setContentEn(strContentEn);
                            this.activityServiceDao.saveReturn(activity);
                            activities.add(activity);
                        }
                        activity.setAwardGold(nAwardGold);
                        strDatetime = MyUtil.dateFormatYMD_HMS_Sting(new Date());
                        activity.setDatetime(strDatetime);
                        activity.setCondition1(nCondition4);
                        activity.setCondition2(nCondition5);
                        activity.setCondition3(nCondition6);
                        activity.setType(nTopAwardType);
                    }
                    break;
                case 5:
                    nCondition1 = Integer.parseInt(conditions[0]);
                    activity.setCondition1(nCondition1);
                    activity.setCondition2(-1);
                    activity.setCondition3(-1);
                    for (strCondition1 = activityList.iterator(); ((Iterator) strCondition1).hasNext(); ) {
                        Activity a3 = (Activity) ((Iterator) strCondition1).next();
                        if (a3.getCondition1() == activity.getCondition1()) {
                            count++;
                        }
                    }
                    if (count > 0) {
                        result.put("success", Boolean.valueOf(false));
                        result.put("message", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "activityConditionsRepeat"));
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "addActivity", new Object[]{result}));
                        return;
                    }
                    nAwardGold = Integer.parseInt(strAwardGold);
                    activity.setAwardGold(nAwardGold);
                    strContent = "活动期间每日单局赢得" + nCondition1 + "游戏币，赠送" + nAwardGold + "游戏币";
                    strContentEn = "If you can win " + nCondition1 + " coins in a round per day, you will be given " + nAwardGold + " coins as gift during activity";
                    activity.setContent(strContent);
                    activity.setContentEn(strContentEn);
                    strDatetime = MyUtil.dateFormatYMD_HMS_Sting(new Date());
                    activity.setDatetime(strDatetime);
                    this.activityServiceDao.saveReturn(activity);
                    activities.add(activity);
                    break;
                case 6:
                    nCondition1 = Integer.parseInt(conditions[0]);
                    nCondition2 = Integer.parseInt(conditions[1]);
                    activity.setCondition1(nCondition1);
                    activity.setCondition2(nCondition2);
                    activity.setCondition3(-1);
                    for (strCondition1 = activityList.iterator(); ((Iterator) strCondition1).hasNext(); ) {
                        Activity a3 = (Activity) ((Iterator) strCondition1).next();
                        if (a3.getCondition1() == activity.getCondition1()) {
                            count++;
                        }
                    }
                    if (count > 0) {
                        result.put("success", Boolean.valueOf(false));
                        result.put("message", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "activityConditionsRepeat"));
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "addActivity", new Object[]{result}));
                        return;
                    }
                    nAwardGold = Integer.parseInt(strAwardGold);
                    activity.setAwardGold(nAwardGold);
                    strContent = "活动期间每日" + nCondition1 + "点整后5分钟内，进入游戏大厅，即可参与全民抢红包活动，红包总币数" + nAwardGold + "，红包个数为" + nCondition2 + "。";
                    strContentEn = "If you enter game hall witnin 5 minutes after " + nCondition1 + " am./pm. you can snatch a red envelope (with money inside).red envelope all values " + nAwardGold + "red envelope all coins " + nCondition2 + ".";
                    strContent2 = "活动期间每日" + nCondition1 + "点整后5分钟内，进入游戏大厅，即可参与全民抢红包活动。";
                    strContentEn2 = "If you enter game hall witnin 5 minutes after " + nCondition1 + " am./pm. you can snatch a red envelope (with money inside).";
                    activity.setContent(strContent);
                    activity.setContentEn(strContentEn);
                    strDatetime = MyUtil.dateFormatYMD_HMS_Sting(new Date());
                    activity.setDatetime(strDatetime);
                    this.activityServiceDao.saveReturn(activity);
                    activities.add(activity);
            }
            result.put("success", Boolean.valueOf(true));
            result.put("message", "");
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "addActivity", new Object[]{result}));
            Activity activityNull = new Activity();
            pubUpdateActivityNotice(activityNull);
            if (nType == 6) {
                for (int j = 0; j < activities.size(); j++) {
                    activity.setContent(strContent2);
                    activity.setContentEn(strContentEn2);
                }
            }
            new Thread() {
                public void run() {
                    ActivityService activityService = (ActivityService) com.miracle9.game.util.MyApplicationContext.a().getBean("activityService");
                    List<Activity> activities = activityService.getActivityList();
                    for (Activity activity : activities) {
                        activityService.addUserActivity(activity);
                    }
                }
            }.start();
        }
    }

    public void addUserActivity(Activity activity) {
        List<User> users = this.userServiceDao.q_CreateSqlList("from User where promoterId=0", new Object[0]);
        for (User user : users) {
            if (activity.getType() == 3) {
                activity.setCondition2(user.getLevel());
            }
            UserActivity userActivity = new UserActivity(user.getId(), activity.getId(), activity.getContent(), activity.getContentEn(), activity.getAwardGold(), 0, activity.getType(), 0, 0, 0, 0, activity.getCondition1(), activity.getCondition2(), activity.getCondition3());
            this.userActivityServiceDao.saveReturnInteger(userActivity);
            IoSession s = (IoSession) GameParameterAssist.ba.get(Integer.valueOf(user.getId()));
            if (s != null) {
                AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "newUserActivity", new Object[]{Integer.valueOf(activity.getType())}));
            }
        }
    }

    public void deleteActivity(int nActivityId, IoSession session) {
        Map result = new HashMap();
        Activity activity = (Activity) this.activityServiceDao.getById(nActivityId);
        try {
            this.activityServiceDao.deleteById(nActivityId);
            this.userActivityServiceDao.deleteByActivityId(nActivityId);
            result.put("success", Boolean.TRUE);
            result.put("message", "");
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "deleteActivity", new Object[]{result}));
            Map map = new HashMap();
            ActivityInfo activityInfo = this.activityInfoServiceDao.getActivityInfo();
            int nActivityState = activityInfo.getActivityStatus();
            int count = this.activityServiceDao.getQuantity();
            if ((nActivityState == 2) && (count == 0)) {
                updateActivityStatus(0);
                activityInfo.setActivityStatus(0);
                map.put("activityInfo", activityInfo);
                AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "getActivityInfo", new Object[]{map}));
            }
            pubUpdateActivityNotice(activity);
        } catch (Exception e) {
            result.put("success", Boolean.FALSE);
            result.put("message", "");
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "deleteActivity", new Object[]{result}));
        }
    }

    public void updateActivityDate(String strStartDate, String strEndDate, IoSession session) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(new Date());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
        String strNowDate = format.format(c1.getTime());
        if ((strStartDate == "") && (strEndDate == "")) {
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "updateActivityDate", new Object[1]));
        }
        Map result = new HashMap();
        ActivityInfo activityInfo = this.activityInfoServiceDao.getActivityInfo();
        ActivityInfo newActivityInfo = new ActivityInfo();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar nowDate = Calendar.getInstance();
        Calendar newDate = Calendar.getInstance();
        if (activityInfo != null) {
            try {
                System.out.println("====================" + activityInfo.getStartDate() + "，" + strStartDate);
                nowDate.setTime(df.parse(strNowDate));
                newDate.setTime(df.parse(strStartDate));
                int nResult = nowDate.compareTo(newDate);
                if (nResult == 0) {
                    newActivityInfo.setActivityStatus(1);
                } else if (nResult < 0) {
                    newActivityInfo.setActivityStatus(0);
                } else {
                    newActivityInfo.setActivityStatus(2);
                }
            } catch (ParseException e) {
                System.err.println("格式不正确");
            }
        }
        newActivityInfo.setStartDate(strStartDate);
        newActivityInfo.setEndDate(strEndDate);
        if (activityInfo == null) {
            this.activityInfoServiceDao.save(newActivityInfo);
        } else {
            activityInfo.setStartDate(newActivityInfo.getStartDate());
            activityInfo.setEndDate(newActivityInfo.getEndDate());
            activityInfo.setActivityStatus(newActivityInfo.getActivityStatus());
            this.activityInfoServiceDao.save(activityInfo);
        }
        updateActivityInfoNotice();
        Activity activity = new Activity();
        pubUpdateActivityNotice(activity);
        result.put("success", Boolean.valueOf(true));
        result.put("activityInfo", newActivityInfo);
        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "updateActivityDate", new Object[]{result}));
        Query queryObject = this.userActivityServiceDao.s_CreateSql("truncate table user_activity", new Object[0]);
        queryObject.executeUpdate();
        new Thread() {
            public void run() {
                ActivityService activityService = (ActivityService) com.miracle9.game.util.MyApplicationContext.a().getBean("activityService");
                List<Activity> activities = activityService.getActivityList();
                for (Activity activity : activities) {
                    activityService.addUserActivity(activity);
                }
            }
        }.start();
    }

    public void updateActivityInfoNotice() {
        List<User> users = this.userServiceDao.q_CreateSqlList("from User where promoterId=0", new Object[0]);
        for (User user : users) {
            IoSession s = (IoSession) GameParameterAssist.ba.get(Integer.valueOf(user.getId()));
            if (s != null) {
                AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "updateActivityInfo", new Object[0]));
            }
        }
    }

    public Map<String, Object> getInActivity(IoSession session) {
        int nUserId = ((Integer) session.getAttribute("id")).intValue();
        Map result = new HashMap();
        ActivityInfo activityInfo = this.activityInfoServiceDao.getActivityInfo();
        if (activityInfo == null) {
            result.put("success", Boolean.valueOf(false));
        } else if ((activityInfo.getActivityStatus() == 0) || (activityInfo.getActivityStatus() == 2)) {
            result.put("success", Boolean.valueOf(false));
        } else {
            result.put("startDate", activityInfo.getStartDate());
            result.put("endDate", activityInfo.getEndDate());
            int[] isNewFinishs = new int[6];
            for (int i = 0; i < isNewFinishs.length; i++) {
                int nType = i + 1;
                List<UserActivity> userActivities = this.userActivityServiceDao.getUserActivitysByUserIdAndType(nUserId, nType);
                if (userActivities.size() == 0) {
                    isNewFinishs[i] = 2;
                } else {
                    int count = 0;
                    for (UserActivity userActivity : userActivities) {
                        if (userActivity.getStatus() == 1) {
                            count++;
                        }
                    }
                    if (count >= 1) {
                        isNewFinishs[i] = 1;
                    } else if (count == 0) {
                        isNewFinishs[i] = 0;
                    }
                }
            }
            result.put("isNewFinish", isNewFinishs);
            for (int i = 0; i < isNewFinishs.length; i++) {
                int nType = i + 1;
                List userActivities2 = this.userActivityServiceDao.getUserActivitysByUserIdAndType(nUserId, nType);
                if (userActivities2.size() != 0) {
                    result.put("userActivities", userActivities2.toArray(new UserActivity[0]));
                    break;
                }
            }
            result.put("success", Boolean.valueOf(true));
        }
        return result;
    }

    public void getUserActivityList(int nType, IoSession session) {
        int nUserId = ((Integer) session.getAttribute("id")).intValue();
        List<UserActivity> userActivityList = this.userActivityServiceDao.getUserActivitysByUserIdAndType(nUserId, nType);
        UserActivity[] arrUserActivity = (UserActivity[]) userActivityList.toArray(new UserActivity[0]);
        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "getUserActivityList", new Object[]{Integer.valueOf(nType), arrUserActivity}));
    }

    public void getActivity(int nType, IoSession session) {
        int nUserId = ((Integer) session.getAttribute("id")).intValue();
        List userActivityList = this.userActivityServiceDao.getUserActivitysByUserIdAndType(nUserId, nType);
        UserActivity[] arrUserActivity = (UserActivity[]) userActivityList.toArray(new UserActivity[0]);
        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "getActivity", new Object[]{Integer.valueOf(nType), arrUserActivity}));
    }

    public List<UserActivity> getAllUserActivity(int nType) {
        List userActivityList = this.userActivityServiceDao.getAllUserActivity(nType);
        return userActivityList;
    }

    public List<Activity> getActivityList(int nType) {
        List activityList = this.activityServiceDao.getActivitysByIdSQL(nType);
        return activityList;
    }

    public Map<String, Object> getNewAward(int nUserActivityId, int nAwardGold, IoSession session) {
        Map result = new HashMap();
        UserActivity userActivity = (UserActivity) this.userActivityServiceDao.getUserActivityByUserId(Integer.valueOf(nUserActivityId));
        if (userActivity.getStatus() == 1) {
            int nStatus = 2;
            boolean bSuccess = this.userActivityServiceDao.updateStatusById(2, nUserActivityId);
            if (bSuccess) {
                int nUserId = ((Integer) session.getAttribute("id")).intValue();
                this.userServiceDao.updateGameById(Integer.valueOf(nUserId), nAwardGold, 0);
                User user = (User) this.userServiceDao.getById(Integer.valueOf(nUserId));
                UserService.pubGoldNotice(user);
                int nNewFinishActivity = this.userActivityServiceDao.q_CreateSqlInt("select count(*) from UserActivity where userId=? and status = 1", new Object[]{Integer.valueOf(nUserId)});
                if (nNewFinishActivity > 0) {
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "newFinishActivity", new Object[]{Boolean.valueOf(true)}));
                } else if (nNewFinishActivity == 0) {
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "newFinishActivity", new Object[]{Boolean.valueOf(false)}));
                }
                int nNewActivityDisplay = this.userActivityServiceDao.q_CreateSqlInt("select count(*) from UserActivity where type=? and userId=? and status = 1 ", new Object[]{Integer.valueOf(userActivity.getType()), Integer.valueOf(nUserId)});
                if (nNewActivityDisplay > 0) {
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "newActivityDisplay", new Object[]{Integer.valueOf(userActivity.getType()), Boolean.valueOf(true)}));
                } else if (nNewActivityDisplay == 0) {
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "newActivityDisplay", new Object[]{Integer.valueOf(userActivity.getType()), Boolean.valueOf(false)}));
                }
                publicMethod(userActivity, user, nAwardGold);
            }
            result.put("success", Boolean.valueOf(bSuccess));
            result.put("userActivityId", Integer.valueOf(nUserActivityId));
            return result;
        }
        result.put("success", Boolean.valueOf(false));
        result.put("userActivityId", Integer.valueOf(nUserActivityId));
        return result;
    }

    public void getBonus(int nUserId, IoSession session) {
        Map result = new HashMap();
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(11);
        int nType = 6;
        int nCondition1 = hour;
        int nAwardGold = 0;
        UserActivity userActivity = this.userActivityServiceDao.getByUserIdAndTypeAndNCondition1(nUserId, 6, nCondition1);
        User user = new User();
        switch (userActivity.getBonusCount()) {
            case 0:
                nAwardGold = redEnvelopeAlgorithm(nUserId);
                if (nAwardGold != -1) {
                    result.put("bonusCount", Integer.valueOf(1));
                    result.put("userId", Integer.valueOf(nUserId));
                    result.put("awardGold", Integer.valueOf(nAwardGold));
                    this.userActivityServiceDao.updateBonusCountById(userActivity.getId(), 1);
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "getBonus", new Object[]{result}));
                    this.userServiceDao.updateGameById(Integer.valueOf(nUserId), nAwardGold, 0);
                    user = (User) this.userServiceDao.getById(Integer.valueOf(nUserId));
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "newGameGold", new Object[]{Integer.valueOf(user.getGameGold())}));
                }
                break;
            case 1:
                nAwardGold = redEnvelopeAlgorithm(nUserId);
                if (nAwardGold != -1) {
                    result.put("bonusCount", Integer.valueOf(2));
                    result.put("userId", Integer.valueOf(nUserId));
                    result.put("awardGold", Integer.valueOf(nAwardGold));
                    this.userActivityServiceDao.updateBonusCountById(userActivity.getId(), 2);
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "getBonus", new Object[]{result}));
                    this.userServiceDao.updateGameById(Integer.valueOf(nUserId), nAwardGold, 0);
                    user = (User) this.userServiceDao.getById(Integer.valueOf(nUserId));
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "newGameGold", new Object[]{Integer.valueOf(user.getGameGold())}));
                }
                break;
            case 2:
                nAwardGold = redEnvelopeAlgorithm(nUserId);
                if (nAwardGold != -1) {
                    result.put("bonusCount", Integer.valueOf(3));
                    result.put("userId", Integer.valueOf(nUserId));
                    result.put("awardGold", Integer.valueOf(nAwardGold));
                    this.userActivityServiceDao.updateBonusCountById(userActivity.getId(), 3);
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "getBonus", new Object[]{result}));
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "overBonusRain", new Object[0]));
                    this.userServiceDao.updateGameById(Integer.valueOf(nUserId), nAwardGold, 0);
                    user = (User) this.userServiceDao.getById(Integer.valueOf(nUserId));
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "newGameGold", new Object[]{Integer.valueOf(user.getGameGold())}));
                }
                break;
        }
        if (nAwardGold != 0) {
            publicMethod(userActivity, user, nAwardGold);
        }
    }

    public void updateAllBonusCount() {
        this.userActivityServiceDao.updateBonusCount_0();
    }

    public int redEnvelopeAlgorithm(int nUserId) {
        int nBonus = 0;
        if (GameParameterAssist.aG.size() == 0) {
            nBonus = -1;
            for (Map.Entry entry : GameParameterAssist.aR.entrySet()) {
                AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity((IoSession) entry.getValue(), "overBonusRain", new Object[0]));
                AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity((IoSession) entry.getValue(), "startBonusRain", new Object[]{Integer.valueOf(-1)}));
            }
        } else {
            nBonus = ((Integer) GameParameterAssist.aG.get(0)).intValue();
            GameParameterAssist.aG.remove(0);
        }
        if (nBonus >= 300) {
            User user = (User) this.userServiceDao.getById(Integer.valueOf(nUserId));
            String strContent = "恭喜" + user.getUsername() + "抢到" + nBonus + "游戏币的大红包，真是运气来了，挡都挡不住！";
            GameService.sendNotice(strContent);
        }
        return nBonus;
    }

    public void redEnvelope(int nHour) {
        Activity activity = (Activity) this.activityServiceDao.q_CreateSqlFilter("from Activity where  type=6 and condition1= ?", new Object[]{Integer.valueOf(nHour)});
        int sumValue = activity.getAwardGold();
        int num = activity.getCondition2();
        com.miracle9.game.util.scoreResultProcess hongbao = new com.miracle9.game.util.scoreResultProcess();
        hongbao.addInteger(sumValue, num);
    }

    public static void main(String[] args) {
        ActivityService s = new ActivityService();
        s.redEnvelope(2);
    }

    public boolean updateActivityStatus(int activityState) {
        return this.activityInfoServiceDao.updateActivityStatus(activityState);
    }

    public void updateUserActivityStatus(int nActivityState) {
        this.userActivityServiceDao.updateStatus_0(nActivityState);
    }

    public void cleanSumYa() {
        this.userActivityServiceDao.updateSumInfo();
    }

    public void pubUpdateActivityNotice(Activity activity) {
        if (activity.getType() != 0) {
            List<User> users = this.userServiceDao.q_CreateSqlList("from User where promoterId=0", new Object[0]);
            for (User user : users) {
                IoSession s = GameParameterAssist.ba.get(user.getId());
                if (s != null) {
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "newUserActivity", new Object[]{activity.getType()}));
                }
            }
        }
        for (Set<IoSession> ioSessions : SessionUtils.getAllSession()) {
            for (IoSession s : ioSessions) {
                AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "manageService/updateActivity", new Object[0]));
            }
        }
    }

    public boolean checkPayUserActivity(int nUserId, int nPayGold, int nCheckType) {
        boolean bIsFinish = false;
        int nActivityType = 1;
        ActivityInfo activityInfo = this.activityInfoServiceDao.getActivityInfo();
        if (activityInfo == null) {
            return false;
        }
        if (activityInfo.getActivityStatus() != 1) {
            return false;
        }
        User user = (User) this.userServiceDao.getById(Integer.valueOf(nUserId));
        List<UserActivity> payActivityList = new ArrayList();
        if ((user.getPromoterId() == 0) && (user.getType() == 0)) {
            payActivityList = new ArrayList();
            payActivityList = this.userActivityServiceDao.getUserActivitysByUserIdAndType(nUserId, 1);
            if (payActivityList.size() == 0) {
            }
        } else {
            UserActivity userActivity;
            switch (nCheckType) {
                case 1:
                    if (nPayGold < ((UserActivity) payActivityList.get(0)).getCondition1()) {
                        bIsFinish = false;
                    }
                    Integer nUserActivityId = null;
                    userActivity = new UserActivity();
                    for (UserActivity payUserActivity : payActivityList) {
                        if (nPayGold >= payUserActivity.getCondition1()) {
                            nUserActivityId = Integer.valueOf(payUserActivity.getId());
                            userActivity = payUserActivity;
                        }
                    }
                    if ((nUserActivityId != null) && (userActivity.getStatus() == 0) && (userActivity.getCondition3() == -1)) {
                        userActivity.setCondition3(1);
                        if (userActivity.getPayBeforeSumYa() == 0) {
                            int nPayBeforeSumYa = this.userServiceDao.getSumYaGoldByUid(nUserId);
                            userActivity.setPayBeforeSumYa(nPayBeforeSumYa);
                            this.userActivityServiceDao.update(userActivity);
                        }
                    }
                    break;
                case 2:
                    for (UserActivity payUserActivity : payActivityList) {
                        if (payUserActivity.getCondition3() != -1) {
                            if ((payUserActivity.getCondition3() == 1) && (payUserActivity.getStatus() == 0)) {
                                int nPayAfterSumYa = this.userServiceDao.getSumYaGoldByUid(nUserId);
                                int nSumYa = nPayAfterSumYa - payUserActivity.getPayBeforeSumYa();
                                payUserActivity.setDaySumYa(nSumYa);
                                this.userActivityServiceDao.update(payUserActivity);
                                if ((nSumYa >= payUserActivity.getCondition2()) && (nSumYa > 0)) {
                                    payUserActivity.setDaySumYa(payUserActivity.getCondition2());
                                    payUserActivity.setStatus(1);
                                    this.userActivityServiceDao.update(payUserActivity);
                                    bIsFinish = true;
                                }
                            } else {
                                bIsFinish = false;
                            }
                        }
                    }
                default:
                    bIsFinish = false;
                    break;
            }
        }
        return bIsFinish;
    }

    public boolean checkPlayUserActivity(int nUserId) {
        boolean bIsFinish = false;
        int nActivityType = 2;
        ActivityInfo activityInfo = this.activityInfoServiceDao.getActivityInfo();
        if (activityInfo.getActivityStatus() != 1) {
            return false;
        }
        User user = (User) this.userServiceDao.getById(Integer.valueOf(nUserId));
        if ((user.getPromoterId() == 0) && (user.getType() == 0)) {
            List<UserActivity> playActivityList = new ArrayList();
            playActivityList = this.userActivityServiceDao.getUserActivitysByUserIdAndType(nUserId, 2);
            if (playActivityList.size() != 0) {
                int daySumYa = 0;
                Calendar c1 = Calendar.getInstance();
                c1.setTime(new Date());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String startDate = format.format(c1.getTime());
                String endDate = format.format(c1.getTime());
                endDate = MyUtil.DateByDayCalendar(endDate, 1);
                String hql = "";
                hql = "select ifnull(sum(ya_gold),0) ,ifnull(sum(de_gold),0) from user_ten_min_score where user_id=" + nUserId + " and start_time>=? and end_time<? ";
                List<Object[]> log = this.userServiceDao.returnList_3(hql, startDate, endDate);
                for (Object[] o1 : log) {
                    daySumYa = Integer.parseInt(o1[0].toString());
                }
                if (daySumYa < ((UserActivity) playActivityList.get(0)).getCondition1()) {
                    bIsFinish = false;
                }
                Integer nUserActivityId = null;
                UserActivity userActivity = new UserActivity();
                for (UserActivity payUserActivity : playActivityList) {
                    this.userActivityServiceDao.update(payUserActivity);
                    if (daySumYa < payUserActivity.getCondition1()) {
                        payUserActivity.setDaySumYa(daySumYa);
                    }
                    if (daySumYa >= payUserActivity.getCondition1()) {
                        nUserActivityId = Integer.valueOf(payUserActivity.getId());
                        userActivity = payUserActivity;
                        if (nUserActivityId != null) {
                            if (userActivity.getStatus() == 0) {
                                int nStatus = 1;
                                boolean bSuccess = this.userActivityServiceDao.updateStatusById(1, userActivity.getId());
                                if (bSuccess) {
                                    payUserActivity.setDaySumYa(payUserActivity.getCondition1());
                                    payUserActivity.setStatus(1);
                                    this.userActivityServiceDao.update(payUserActivity);
                                    bIsFinish = true;
                                } else {
                                    bIsFinish = false;
                                }
                            }
                        }
                    }
                }
            }
        }
        return bIsFinish;
    }

    public boolean checkLevelUserActivity(User user) {
        boolean bIsFinish = false;
        int nActivityType = 3;
        ActivityInfo activityInfo = this.activityInfoServiceDao.getActivityInfo();
        if (activityInfo.getActivityStatus() != 1) {
            return false;
        }
        if ((user.getPromoterId() == 0) && (user.getType() == 0)) {
            List<UserActivity> levelActivityList = new ArrayList();
            levelActivityList = this.userActivityServiceDao.getUserActivitysByUserIdAndType(user.getId(), 3);
            if (levelActivityList.size() != 0) {
                int nCurrentLevel = user.getLevel();
                if (nCurrentLevel < ((UserActivity) levelActivityList.get(0)).getCondition1()) {
                    bIsFinish = false;
                }
                if ((nCurrentLevel >= ((UserActivity) levelActivityList.get(0)).getCondition1()) && (nCurrentLevel > ((UserActivity) levelActivityList.get(0)).getCondition2())) {
                    Integer nUserActivityId = null;
                    UserActivity userActivity = new UserActivity();
                    for (UserActivity payUserActivity : levelActivityList) {
                        if (nCurrentLevel >= payUserActivity.getCondition1()) {
                            nUserActivityId = Integer.valueOf(payUserActivity.getId());
                            userActivity = payUserActivity;
                            if (nUserActivityId != null) {
                                if (userActivity.getStatus() == 0) {
                                    int nStatus = 1;
                                    boolean bool1 = bIsFinish = this.userActivityServiceDao.updateStatusById(1, userActivity.getId());
                                }
                            }
                        }
                    }
                }
            }
        }
        return bIsFinish;
    }

    public void updateLevelCondition2(int nLevel, int nUserId, int nType) {
        this.userActivityServiceDao.updateCondition2(nLevel, nUserId, nType);
    }

    public void checkUserTopActivity(int nUserId, int nActivityId) {
        UserActivity userActivity = this.userActivityServiceDao.getByUserIdAndActivityId(nUserId, nActivityId);
        if (userActivity.getStatus() == 0) {
            this.userActivityServiceDao.updateStatusById(1, userActivity.getId());
        }
    }

    public boolean checkSingleUserActivity(int nUserId, int nDeGold, int nActivityId) {
        boolean bIsFinish = false;
        int nActivityType = 5;
        ActivityInfo activityInfo = this.activityInfoServiceDao.getActivityInfo();
        if (activityInfo.getActivityStatus() != 1) {
            return false;
        }
        User user = (User) this.userServiceDao.getById(Integer.valueOf(nUserId));
        if ((user.getPromoterId() == 0) && (user.getType() == 0)) {
            UserActivity userActivity = new UserActivity();
            userActivity = this.userActivityServiceDao.getByUserIdAndActivityId(nUserId, nActivityId);
            List<UserActivity> singleUserActivityList = new ArrayList();
            singleUserActivityList = this.userActivityServiceDao.getUserActivitysByUserIdAndType(user.getId(), 5);
            if (singleUserActivityList.size() != 0) {
                for (UserActivity singleUserActivity : singleUserActivityList) {
                    if (singleUserActivity.getCondition1() <= nDeGold) {
                        if (singleUserActivity.getStatus() == 0) {
                            int nStatus = 1;
                            boolean bool1 = bIsFinish = this.userActivityServiceDao.updateStatusById(1, singleUserActivity.getId());
                        }
                    }
                }
            }
        }
        return bIsFinish;
    }

    public void cleanUserActivityStatus() {
        int nStatus = 0;
        this.userActivityServiceDao.updateStatusByType(0);
        this.userActivityServiceDao.updateDaySumYa_0ByType_2();
    }

    public void publicMethod(UserActivity userActivity, User user, int nAwardGold) {
        String strActivityType = "";
        switch (userActivity.getType()) {
            case 1:
                strActivityType = "充值奖励";
                break;
            case 2:
                strActivityType = "累计游玩";
                break;
            case 3:
                strActivityType = "升级奖励";
                break;
            case 4:
                strActivityType = "排行奖励";
                break;
            case 5:
                strActivityType = "单局奖励";
                break;
            case 6:
                strActivityType = "万元红包来袭";
        }
        UserAward userAward = new UserAward();
        userAward.setUserId(user.getId());
        userAward.setUsername(user.getUsername());
        userAward.setGold(nAwardGold);
        userAward.setStatus(1);
        userAward.setDatetime(MyUtil.dateFormatYMD_HMS_Sting(new Date()));
        userAward.setAdmin("admin");
        userAward.setAuthority(0);
        this.userAwardServiceDao.save(userAward);
        this.userWarningServiceDao.updateGiveInfoByUid(user.getId(), nAwardGold);
        AdminLog adminLog = new AdminLog("admin", String.valueOf(user.getUsername()) + "完成" + strActivityType + "活动，赠送" + nAwardGold + "游戏币", 0, 0, "The operation of present coins has been operated for the member which ID is " + user.getUsername() + " ,a total of " + nAwardGold + " coins");
        this.adminLogServiceDao.addAdminLog(adminLog);
        SystemConfig config = this.systemConfigServiceDao.getSystemConfig();
        boolean isOverflow = false;
        double userMoney = MyUtil.goldExchange(user.getGameGold(), config.getPayScale());
        if ((config.getMoneyOverrun() != -1) && (userMoney >= config.getMoneyOverrun()) && (user.getOverflow() == 0)) {
            this.userServiceDao.updateOverflow(user.getId(), 1);
            isOverflow = true;
        }
        if (isOverflow) {
            UserService.pubOverflowNotice(user.getId());
        }
    }

}
